/*
 * Copyright(c) 2018 优居科技 All rights reserved.
 * distributed with this file and available online at
 * http://www.yjyz.com/
 */
package com.zb.api.business.service.impl;


import com.zb.api.business.dto.PosPlanCommentDto;
import com.zb.api.business.dto.PosPlanDto;
import com.zb.api.business.entity.PosMerchantEntity;
import com.zb.api.business.entity.PosPlanCommentEntity;
import com.zb.api.business.entity.UserEntity;
import com.zb.api.business.mapper.PosMerchantMapper;
import com.zb.api.business.mapper.PosPlanCommentMapper;
import com.zb.api.business.mapper.PosPlanMapper;
import com.zb.api.business.service.IPosPlanCommentService;
import com.zb.api.common.models.Page;
import com.zb.api.utils.UserThreadLocal;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
* 方案评论表
 *
 * @author cwp
 * @version v1.0
 * @CreationTime: - 2022-02-08 14:19:31
 * @Description:
 */
@Service
public class PosPlanCommentServiceImpl implements IPosPlanCommentService {

    @Autowired
    private PosPlanCommentMapper posPlanCommentMapper;

    @Autowired
    private PosMerchantMapper posMerchantMapper;

    @Autowired
    private PosPlanMapper posPlanMapper;

    @Override
    public Page<PosPlanCommentDto> getPosPlanCommentList(int pageNo, int pageSize, PosPlanCommentDto posPlanCommentDto){
        /*PageHelper.startPage(pageNo, pageSize);
        PosPlanCommentEntity posPlanCommentEntity = BeansUtils.copyProperties(posPlanCommentDto, PosPlanCommentEntity.class);
        return BeanUtils.toPage(posPlanCommentMapper.getList(posPlanCommentEntity), PosPlanCommentDto.class);*/
        return null;
    }

    @Override
    public PosPlanCommentDto get(String id) {
        PosPlanCommentDto posPlanCommentDto = new PosPlanCommentDto();
        BeanUtils.copyProperties(posPlanCommentMapper.get(id), posPlanCommentDto);
        return posPlanCommentDto;
    }

    @Override
    public int update(PosPlanCommentDto posPlanComment){
        PosPlanCommentEntity entity = new PosPlanCommentEntity();
        BeanUtils.copyProperties(posPlanComment, entity);
        return posPlanCommentMapper.update(entity);
    }

    @Override
    public Integer insert(PosPlanCommentDto posPlanComment){
        PosPlanCommentEntity entity = new PosPlanCommentEntity();
        BeanUtils.copyProperties(posPlanComment, entity);
        return entity.getId();
    }

    @Override
    public int delete(PosPlanCommentDto posPlanComment){
        return posPlanCommentMapper.delete(posPlanComment);
    }

    @Override
    public Integer insertPlanComment(PosPlanCommentDto posPlanCommentDto) throws IllegalAccessException {
        UserEntity user =  UserThreadLocal.get();

        /*PosMerchantEntity posMerchantEntity =  posMerchantMapper.getMerchant(user.getId());
        if (posMerchantEntity!=null){
            PosPlanDto planDto = new PosPlanDto();
            planDto.setMerchantId(posMerchantEntity.getId());
            List<PosPlanDto> posPlanDtos = posPlanMapper.getList(planDto);
            if (!CollectionUtils.isEmpty(posPlanDtos)){
                throw new IllegalAccessException("用户不能评论自己商户下的方案");
            }
        }*/
        if (posPlanCommentDto.getType()==1){
            posPlanCommentDto.setTargetId(posPlanCommentDto.getMerchantId());
            PosPlanCommentDto commentDto = new PosPlanCommentDto();
            commentDto.setType(1);
            commentDto.setUserId(user.getId());
            commentDto.setPlanId(posPlanCommentDto.getPlanId());
            List<PosPlanCommentDto> planCommentDtos = posPlanCommentMapper.getList(commentDto);
            if (!CollectionUtils.isEmpty(planCommentDtos)){
                throw new IllegalAccessException("不能在同一个方案下重复评论");
            }
        }

        PosPlanCommentEntity entity = new PosPlanCommentEntity();
        entity.setCreateTm(new Date().getTime());
        BeanUtils.copyProperties(posPlanCommentDto, entity);
        entity.setUserId(user.getId());
        entity.setUserName(user.getUsername());
        entity.setUserPhone(user.getPhone());
        posPlanCommentMapper.insert(entity);
        return entity.getId();
    }


    @Override
    public Integer delPlanComment(Integer id) {
        PosPlanCommentDto posPlanCommentDto = new PosPlanCommentDto();
        posPlanCommentDto.setId(id);
        List<PosPlanCommentDto> list = new ArrayList<>();
        list.add(posPlanCommentDto);
        delPlanList(list);
        return 1;
    }

    @Override
    public Page<PosPlanCommentDto> getList(PosPlanCommentDto posPlanCommentDto) {
        if (posPlanCommentDto.getQueryType()==1){
            UserEntity user =  UserThreadLocal.get();
            posPlanCommentDto.setUserId(user.getId());
        }
        Page<PosPlanCommentDto> page = new Page<>();
        posPlanCommentDto.setType(1);
        List<PosPlanCommentDto> list = getPlanList(posPlanCommentMapper.getList(posPlanCommentDto));
        page.setList(list);
        page.setPageNo(posPlanCommentDto.getPageNo());
        page.setRowCntPerPage(posPlanCommentDto.getPageSize());
        return page;
    }

    public List<PosPlanCommentDto> getPlanList(List<PosPlanCommentDto> list){
        for (PosPlanCommentDto planCommentDto : list) {
            PosPlanCommentDto commentDto = new PosPlanCommentDto();
            commentDto.setTargetId(planCommentDto.getId());
            commentDto.setType(2);
            List<PosPlanCommentDto> commentDtos = posPlanCommentMapper.getList(commentDto);
            if (!CollectionUtils.isEmpty(commentDtos)){
                planCommentDto.setChildrenList(commentDtos);
                getPlanList(commentDtos);
            }
        }
        return list;
    }

    public List<PosPlanCommentDto> delPlanList(List<PosPlanCommentDto> list){
        for (PosPlanCommentDto planCommentDto : list) {
            PosPlanCommentDto commentDto = new PosPlanCommentDto();
            commentDto.setTargetId(planCommentDto.getId());
            List<PosPlanCommentDto> commentDtos = posPlanCommentMapper.getList(commentDto);
            posPlanCommentMapper.deleteByTarget(planCommentDto);
            if (!CollectionUtils.isEmpty(commentDtos)){
                delPlanList(commentDtos);
            }
        }
        return list;
    }


}
